<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Inspection\Repository\Inspection_devices;

use Plugin\Attractions\Inspection\Model\Inspection_devices\InspectionDevices;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 设备 Repository类
 */
class InspectionDevicesRepository extends IRepository
{
   public function __construct(
        protected readonly InspectionDevices $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 设备名称
        if (isset($params['name']) && !empty($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 设备型号
        if (isset($params['model']) && !empty($params['model'])) {
            $query->where('model', 'like', '%'.$params['model'].'%');
        }

        // 经度
        if (isset($params['longitude']) && !empty($params['longitude'])) {
            $query->where('longitude', '=', $params['longitude']);
        }

        // 纬度
        if (isset($params['latitude']) && !empty($params['latitude'])) {
            $query->where('latitude', '=', $params['latitude']);
        }

        // 海拔高度
        if (isset($params['altitude']) && !empty($params['altitude'])) {
            $query->where('altitude', '=', $params['altitude']);
        }

        // 设备类型ID
        if (isset($params['type_id']) && !empty($params['type_id'])) {
            $query->where('type_id', '=', $params['type_id']);
        }

        // 当前状态 1在线 2离线
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 供应商ID
        if (isset($params['vendor_id']) && !empty($params['vendor_id'])) {
            $query->where('vendor_id', '=', $params['vendor_id']);
        }

        // 服务商ID
        if (isset($params['service_id']) && !empty($params['service_id'])) {
            $query->where('service_id', '=', $params['service_id']);
        }

        // 关联设备
        if (isset($params['device']) && !empty($params['device'])) {
            $query->where('device', 'like', '%'.$params['device'].'%');
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}